package com.a2.appa3.presenter.activity;

import android.content.Intent;
import android.widget.Toast;

import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.ResCode;
import com.a2.appa3.model.network.entity.req.ResLogin;
import com.a2.appa3.model.network.entity.req.Resberifystatus;
import com.a2.appa3.model.network.entity.req.Restyelogin;
import com.a2.appa3.ui.activity.LoginActivity;
import com.a2.appa3.ui.activity.PasswordActivity;
import com.a2.appa3.util.SpUtils;
import com.a2.appa3.widget.CheckOutView;
import com.google.gson.Gson;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivityPresenter extends BaseActivityPresenter<LoginActivity> {
    public LoginActivityPresenter(LoginActivity mActivity) {
        super(mActivity);
    }

    /**
     * 验证状态，取决数字还是滑动验证
     * 默认为0滑块模式，1为数字验证模式
     */
    public void getberifystatus() {
        HttpJsonUtils.getHttpApiInstance()
                .getberifystatus(HttpAdress.getuseAdress() + "api/m/lottery/getSourceData")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resberifystatus>() {
                    @Override
                    protected void onCustomNext(Resberifystatus resberifystatus) {
                        if (resberifystatus.code == 0) {
                            mActivity.berifystatusback(resberifystatus.data.LoginVerType);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });

    }

    /**
     * 滑动验证
     */
    public void getSlideVerify() {
        HttpJsonUtils.getHttpApiInstance()
                .tyelogin(HttpAdress.getuseAdress() + "api/m/SlideVerify")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Restyelogin>() {
                    @Override
                    protected void onCustomNext(Restyelogin restyelogin) {
                        mActivity.SlideVerifyback(restyelogin);
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });

    }

    /**
     * 数字验证码
     */
    public void getCode() {
        HttpJsonUtils.getHttpApiInstance()
                .getcode(HttpAdress.getuseAdress() + "api/m/captcha", Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResCode>(mActivity) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }

                    @Override
                    protected void onCustomNext(ResCode resCode) {
                        if (null != resCode.data)
                            mActivity.onCode(resCode);
                    }
                });
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @param value
     * @param passport
     */
    public void login(String username, String password, String value, String passport) {
        //这里开始网络请求
        HttpJsonUtils.getHttpApiInstance()
                .login(HttpAdress.getuseAdress() + "api/m/login", username, password, value, passport, Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResLogin>() {
                    @Override
                    protected void onCustomNext(ResLogin resLogin) {

                        if (resLogin.code == 0 && resLogin.data != null) {
                            Info.user = resLogin.data;
                            Info.Token = resLogin.data.token;
                            SpUtils.clearUser(mActivity);
                            SpUtils.putString(mActivity, "User", new Gson().toJson(resLogin.data));
                            String user = SpUtils.getString(mActivity, "User");
                            if (new Gson().fromJson(user, ResLogin.DataBean.class) == null) {
                                String user1 = SpUtils.getString(mActivity, "User");
                                if (new Gson().fromJson(user1, ResLogin.DataBean.class) == null) {
                                }
                            }
                            if (resLogin.data.isPhone.equals("1")) {
                            } else {
                                Toast.makeText(mActivity, "请绑定手机号", Toast.LENGTH_SHORT).show();
                            }
                            if (resLogin.data.forcePasswordChange.equals("1")) {
                                Toast.makeText(mActivity, "您的密码不符合规定，请尽快修改", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(mActivity, PasswordActivity.class);
                                mActivity.startActivity(intent);
                            } else {
                                mActivity.onsuccess();
                            }


                        } else {
                            mActivity.onfail(resLogin);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }
}
